/* eslint-disable no-console */
import { redisUser } from './redisOperate';
import { UserRedisType, UserRedisDataType } from '../../types/redis';

// const test = async (key: string): Promise<void> => {
//     await new Promise<void>(resolve => {
//         setTimeout(() => {
//             resolve();
//         }, 1000);
//     });
//     const res = await redisUser.setString(key, '存入一个value');
//     if (res != null) {
//         console.log('写入成功');
//     } else {
//         console.log('写入失败');
//     }
//     const res2 = await redisUser.getString(key);
//     if (res2 != null) {
//         console.log(`读取成功，value = ${res2}`);
//     } else {
//         console.log('读取失败');
//     }
// };

// test('mykey');

const userActivationRedis: UserRedisType = {
    setActivationInfo: async (data: UserRedisDataType) => {
        // 读取 email 的值
        const getResult = await userActivationRedis.getActivationInfo(data.email);
        if (getResult) {
            // 已存在
            return false;
        }
        // 写入用户数据，有效时间 5 分钟
        const setResult = await redisUser.setString(data.email, data, 300);
        if (setResult) {
            // 写入成功
            return true;
        }
        return false;
    },
    getActivationInfo: async (email: string) => {
        console.log('get');
        const result = await redisUser.getString(email);
        if (result) {
            console.log(result);
            return JSON.parse(result) as UserRedisDataType;
        }
        return null;
    },
    delActivationInfo: async (email: string) => {
        const result = await redisUser.delString(email);
        if (result) {
            return true;
        }
        return false;
    },
};

export default userActivationRedis;
